/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zorlog.ui;

/**
 * System.nanoTime() based playback controller
 * @author amalikov
 */
class Playback {

	public static final int IDLE = 0;
	public static final int PLAY = 1;
	public static final int PAUSE = 2;
	
	public Playback(double tick) {
		this.tick = tick;
		state = IDLE;
	}

	public int getIndex() {
		return (state == PAUSE) ? pausedIndex : (int)((System.nanoTime() - start) * 1.0E-9 / tick);
	}

	public void play(int index) {
		start = System.nanoTime() - (long)(index * tick * 1.0E9);
		state = PLAY;
	}

	public void pause() {
		if (state == PLAY) {
			pausedIndex = getIndex();
			state = PAUSE;
		} else if (state == PAUSE)
			play(pausedIndex);
	}

	public void stop() {
		state = IDLE;
	}

	public int getState() {
		return state;
	}
	
	private final double tick;
	private long start;
	private int state;
	private int pausedIndex;
}
